package xyz.xtt.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import xyz.xtt.admin.constants.CheckLimitTypeEnum;
import xyz.xtt.admin.entity.TblUser;
import xyz.xtt.admin.service.ICheckUserLimitService;
import xyz.xtt.admin.service.IUserService;
import xyz.xtt.admin.vo.CheckUserVo;
import xyz.xtt.common.starter.cache.CacheManager;

import java.util.*;

@Service
public class CheckUserLimitServiceImpl implements ICheckUserLimitService {

    @Autowired
    private IUserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private CacheManager cacheManager;

    @Override
    public List<CheckUserVo> checkLimit(Long id ,String phone) throws Exception {
         id = getAppUserIdByPhoneOrId(id, phone);
         if (id == null) {
             return  null;
         }
        List<CheckUserVo> res = new ArrayList<>();

        for(CheckLimitTypeEnum item : CheckLimitTypeEnum.values()) {
            String redisKey = getRedisKey(id, item);
            BoundHashOperations<String, String, String> redisOps = stringRedisTemplate.boundHashOps(redisKey);
            Map<String, String> entries = redisOps.entries();
            if(entries != null && !entries.isEmpty()) {
                CheckUserVo checkUserVo = mapToCheckUserVo(entries,item);
                res.add(checkUserVo);
            }
        }
        return res;
    }

    private CheckUserVo mapToCheckUserVo(Map<String, String> map,CheckLimitTypeEnum typeEnum) {
        if (map == null || map.isEmpty()) {
            return  null;
        }
        CheckUserVo checkUserVo = new CheckUserVo();
        checkUserVo.setContinuousFail(Long.parseLong(map.get("continuousFail")));
        checkUserVo.setSuss(Long.parseLong(map.get("suss")));
        checkUserVo.setTotalReq(Long.parseLong(map.get("totalReq")));
        checkUserVo.setTotalFail(Long.parseLong(map.get("totalFail")));
        checkUserVo.setTypeEnum(typeEnum);
        return checkUserVo;
    }


    public boolean delCheckLimitByLimitType(Long id,  CheckLimitTypeEnum limitType ) {
        if (limitType != null) {
            String redisKey = getRedisKey(id, limitType);
            stringRedisTemplate.delete(redisKey);
            return  true;
        }
        return false;
    }



    // 根据用户ID 手机号 获取用户ID
    private Long getAppUserIdByPhoneOrId(Long id, String phone) throws Exception {
        if (id != null) {
            return id;
        }
        if (StringUtils.isNotBlank(phone)) {
            phone = phone.startsWith("86") ? phone : "86"+phone;
            TblUser user = userService.getUserByNameAndPhone("", phone);
            if (user == null) {
                return null;
            }
            return user.getAppUserId();
        }
        return null;
    }

    private String getRedisKey (Long id, CheckLimitTypeEnum limitType) {
        String daySub = DateUtil.format(new Date(), "yyyyMMdd");
        return  String.format("nft:%d:%s:%s", id, daySub, limitType.desc());
    }

    public String getByRedisKey(String redisKey) {
        return  stringRedisTemplate.opsForValue().get(redisKey);

    }

    public Boolean delByRedisKey(String redisKey) {
        return stringRedisTemplate.delete(redisKey);
    }
}
